<template>
  <XtxDialog :title="formDate.id?   '修改地址':'添加地址'" v-model:visible="dialogVisible">
    <div class="address-edit">
      <div class="xtx-form">
        <div class="xtx-form-item">
          <div class="label">收货人：</div>
          <div class="field">
            <input
              class="input"
              placeholder="请输入收货人"
              v-model="formDate.receiver"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">手机号：</div>
          <div class="field">
            <input
              class="input"
              placeholder="请输入手机号"
              v-model="formDate.contact"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地区：</div>
          <div class="field">
            <XtxCity
              placeholder="请选择所在地区"
              @changeCity="changeCity"
              :fullLocation="formDate.fullLocation"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">详细地址：</div>
          <div class="field">
            <input
              class="input"
              placeholder="请输入详细地址"
              v-model="formDate.address"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">邮政编码：</div>
          <div class="field">
            <input
              class="input"
              placeholder="请输入邮政编码"
              v-model="formDate.postalCode"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地址标签：</div>
          <div class="field">
            <input
              class="input"
              placeholder="请输入地址标签，逗号分隔"
              v-model="formDate.addressTags"
            />
          </div>
        </div>
      </div>
    </div>
    <template v-slot:footer>
      <XtxButton type="gray" style="margin-right: 20px" @click="dialogVisible=false">取消</XtxButton>
      <XtxButton type="primary" @click="submit">确认</XtxButton>
    </template>
  </XtxDialog>
</template>
<script>
import { reactive, ref } from "vue";
import {addAddress,updataAddress} from "@/api/order"
import Message from"@/components/library/message"
export default {
  name: "AddressEdit",
  setup(props,{emit}) {
    const dialogVisible = ref(false);
    // 打开函数
    const open = (data) => {
        console.log(data);
        if(data){
             dialogVisible.value = true;
            console.log('存在');
            for (const key in formDate) {
               formDate[key]=data[key]
            }
        }else{
            console.log('不存在');
        dialogVisible.value = true;
      for (const key in formDate) {
       if(key==='isDefault'){
            formDate[key]=1
       }else{
         formDate[key]=null
       }
      
      }
        }
     
    };
    const formDate = reactive({
      receiver: null,
      contact: null,
      provinceCode: null,
      cityCode: null,
      countyCode: null,
      address: null,
      postalCode: null,
      addressTags: null,
      id:null,
      isDefault: 1,
      //城市组件完整显示
      fullLocation: null,
    });
    const changeCity = (result) => {
        console.log(result );
      formDate.provinceCode = result.provinceCode;
      formDate.cityCode = result.cityCode;
      formDate.countyCode = result.countyCode;
      formDate.fullLocation = result.fullLocation;
    };
    const submit=()=>{
    
        if(formDate.id){
            console.log(formDate);
            updataAddress(formDate).then(data=>{
                  dialogVisible.value=false
                    emit('on-success',formDate)
            Message({text:'修改成功',type:'success'})
            })
        }else{
console.log('添加地址');
 addAddress(formDate).then(data=>{
    console.log(data);
            dialogVisible.value=false
            formDate.id=data.result.id
            emit('on-success',formDate)
            Message({text:'添加成功',type:'success'})
        })
        }
       
    }
    return { dialogVisible, open, formDate, changeCity,submit };
  },
};
</script>
<style scoped lang="less">
.xtx-dialog {
  //      :deep(.body){
  //     height:600px;
  //     overflow-y:visible;
  //   }
  :deep(.wrapper) {
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
</style>